﻿using System;
using System.Collections.Generic;
using System.Linq;
using YArchitech.Controls;

namespace YArchitech.Plumbing
{
	public sealed class EquivalentLengthContainer
	{
		public List<EquivalentLength> EquivalentArray
		{
			get
			{
				return this.equivalentArray;
			}
		}

		private EquivalentLengthContainer()
		{
			this.Initialize();
		}

		public void Initialize()
		{
			if (this.equivalentArray != null && this.equivalentArray.Count > 0)
			{
				return;
			}
			try
			{
				this.equivalentArray = new List<EquivalentLength>();
				this.equivalentArray = HYCalSQLData.GetEquivalentValues();
			}
			catch (Exception ex)
			{
				YJKMessageBox.Error(ex.ToString());
			}
		}

		public double GetFittingEquivalentLength(string name, int dn)
		{
			return this.GetEquivalentLength(name, dn);
		}

		public double GetValveEquivalentLength(string name, int dn)
		{
			double result = 0.0;
			if (name.Contains("蝶"))
			{
				result = this.GetEquivalentLength(EquivalentLength.ButterflyValve, dn);
			}
			if (name.Contains("闸"))
			{
				result = this.GetEquivalentLength(EquivalentLength.BrakeValve, dn);
			}
			if (name.Contains("止") || name.Contains("回"))
			{
				result = this.GetEquivalentLength(EquivalentLength.CheckValve, dn);
			}
			return result;
		}

		private double GetEquivalentLength(string name, int dn)
		{
			double result = 0.0;
			if (this.equivalentArray == null || this.equivalentArray.Count < 1)
			{
				return result;
			}
			try
			{
				List<EquivalentLength> list = (from equivalent in this.equivalentArray
				where equivalent.Name == name
				orderby equivalent.EquivalentValue
				select equivalent).ToList<EquivalentLength>();
				if (list != null)
				{
					EquivalentLength equivalentLength = list.Find((EquivalentLength equivalent) => equivalent.DN == dn);
					if (equivalentLength == null)
					{
						result = this.GetDeviationValue(list, dn);
					}
					else
					{
						result = equivalentLength.EquivalentValue;
					}
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Error(ex.ToString());
			}
			return result;
		}

		private double GetDeviationValue(List<EquivalentLength> equivalents, int dn)
		{
			double result = 0.0;
			if (equivalents == null || equivalents.Count < 1)
			{
				return result;
			}
			try
			{
				int num = 0;
				foreach (EquivalentLength equivalentLength in equivalents)
				{
					num++;
					if (equivalentLength.DN > dn)
					{
						break;
					}
				}
				if (num == 1 || num == equivalents.Count)
				{
					result = equivalents[num - 1].EquivalentValue;
				}
				else
				{
					EquivalentLength equivalentLength2 = equivalents[num - 1];
					EquivalentLength equivalentLength3 = equivalents[num - 2];
					result = (equivalentLength2.EquivalentValue - equivalentLength3.EquivalentValue) / (double)(equivalentLength2.DN - equivalentLength3.DN) * (double)(dn - equivalentLength3.DN) + equivalentLength3.EquivalentValue;
				}
			}
			catch (Exception ex)
			{
				YJKMessageBox.Error(ex.ToString());
			}
			return result;
		}

		public static readonly EquivalentLengthContainer Instance = new EquivalentLengthContainer();

		public List<EquivalentLength> equivalentArray;
	}
}
